package com.activiti.advance;

import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.repository.Deployment;

import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.zip.ZipInputStream;

/**
 * @author ldanniel
 */
public class UelProcessDeployment {
    public static void main(String[] args) {
        UelProcessDeployment uelProcessDeployment = new UelProcessDeployment();
        uelProcessDeployment.startAssigneeUel();
    }

    /**
     * 编写代码配置负责人
     */
    public void startAssigneeUel() {
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        RuntimeService runtimeService = processEngine.getRuntimeService();
        //设定负责人，用来替换uel表达式${assignee0}....
        Map<String, Object> assigneeMap = new HashMap<>();
        assigneeMap.put("assignee0", "张三");
        assigneeMap.put("assignee1", "李经理");
        assigneeMap.put("assignee2", "王总经理");
        assigneeMap.put("assignee3", "赵财务");
        //myEvection,启动流程实例
        runtimeService.startProcessInstanceByKey("myEvection", assigneeMap);
        System.out.println("启动流程实例完成");
    }


    /**
     * 将流程模板打包上传
     */
    public void resourceUpload() {
        //创建
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        //repositoryService
        RepositoryService repositoryService = processEngine.getRepositoryService();
        //流程部署
        InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("bpmn/evection.zip");
        assert inputStream != null;
        ZipInputStream zipInputStream = new ZipInputStream(inputStream);
        Deployment deploy = repositoryService.createDeployment().name("出差申请流程-UEL")
                .disableSchemaValidation() //禁用架构验证
                .addZipInputStream(zipInputStream).deploy();
        System.out.println("部署流程的id" + deploy.getId());
        //部署流程的名称为空，是因为压缩包中可能有多个流程图，所以不能统一命名
        System.out.println("部署流程的名称" + deploy.getName());
    }
}
